<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="clearfix">
          <span>章节管理</span>
        </div>
      </template>

      <!-- 搜索表单 -->
      <el-form
        :model="listQuery"
        ref="queryForm"
        :inline="true"
        label-width="68px"
        class="query-form"
      >
        <el-form-item label="章节名称" prop="keyword">
          <el-input
            v-model="listQuery.keyword"
            placeholder="请输入章节名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleSearchList"
          />
        </el-form-item>
        <el-form-item label="所属课程" prop="courseId">
          <el-select
            v-model="listQuery.courseId"
            placeholder="请选择课程"
            clearable
            style="width: 200px"
          >
            <el-option
              v-for="item in courseOptions"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleSearchList">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="action-buttons">
        <el-button
          type="primary"
          @click="handleAddChapter"
        >
          <el-icon><Plus /></el-icon>添加章节
        </el-button>
        <el-button @click="getList">
          <el-icon><Refresh /></el-icon>刷新
        </el-button>
      </div>

      <!-- 数据表格 -->
      <el-table
        v-loading="listLoading"
        :data="list"
        border
        style="width: 100%"
      >
        <el-table-column label="编号" width="100" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="章节名称" min-width="200">
          <template #default="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属课程" min-width="200">
          <template #default="scope">
            <span>{{ scope.row.courseTitle }}</span>
          </template>
        </el-table-column>
        <el-table-column label="章节序号" width="100" align="center">
          <template #default="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="创建时间" width="160" align="center">
          <template #default="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.status | statusFilter">
              {{ scope.row.status | statusTextFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button
              size="small"
              type="primary"
              @click="handleUpdateChapter(scope.$index, scope.row)"
            >
              <el-icon><Edit /></el-icon>编辑
            </el-button>
            <el-button
              size="small"
              type="success"
              @click="handleViewVideos(scope.$index, scope.row)"
            >
              <el-icon><VideoCamera /></el-icon>视频
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDeleteChapter(scope.$index, scope.row)"
            >
              <el-icon><Delete /></el-icon>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="listQuery.page"
        v-model:limit="listQuery.size"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { Plus, Edit, Refresh, Delete, VideoCamera } from '@element-plus/icons-vue'
import { getChapterPage, deleteChapter } from '@/api/edu'
import { getCourseList } from '@/api/course'

const defaultListQuery = {
  page: 1,
  size: 10,
  keyword: null,
  courseId: null
}

export default {
  name: 'ChapterList',
  components: {
    Plus,
    Edit,
    Refresh,
    Delete,
    VideoCamera
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'info',
        1: 'success'
      }
      return statusMap[status]
    },
    statusTextFilter(status) {
      const statusTextMap = {
        0: '禁用',
        1: '启用'
      }
      return statusTextMap[status]
    }
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: 0,
      listLoading: false,
      courseOptions: []
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      // 获取课程列表
      getCourseList().then(response => {
        this.courseOptions = response.data
      })
      // 获取章节列表
      this.getList()
    },
    getList() {
      this.listLoading = true
      getChapterPage(this.listQuery).then(response => {
        this.list = response.records
        this.total = response.total
        this.listLoading = false
      })
    },
    handleSearchList() {
      this.listQuery.page = 1
      this.getList()
    },
    resetQuery() {
      this.listQuery = Object.assign({}, defaultListQuery)
      this.getList()
    },
    handleAddChapter() {
      this.$router.push('/edu/chapter/add')
    },
    handleUpdateChapter(index, row) {
      this.$router.push(`/edu/chapter/update/${row.id}`)
    },
    handleViewVideos(index, row) {
      this.$router.push(`/edu/chapter/${row.id}/videos`)
    },
    handleDeleteChapter(index, row) {
      this.$confirm('是否要删除该章节?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteChapter(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.action-buttons {
  display: flex;
  justify-content: flex-end;
  margin-top: -10px;
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>